addpath(genpath(pwd))
clear
%% Model specific part
unitFree = 1;
[f,x,xp,y,yp,symparams] = DSGEmodel_yieldCurve(unitFree);

% The number of endogenous states (mx) and number of lagged controls (myx)
mx  = 0;
myx = 1;
%% The analytical derivatives of the model
% The dimension of the model
ny  = size(y,2);

% The parameters of the model
modelParams = cell(1,length(symparams));
for i=1:length(symparams)
    modelParams{i} = char(symparams(i));
end

% Remove exogenous shocks
fall         = f;    
f            = f(1:ny+mx,1);
x11          = x(1,1:mx);
x11p         = xp(1,1:mx);
x12(1,1:myx) = x(1,mx+1:mx+myx);
x12p(1,1:myx)= xp(1,mx+1:mx+myx);

% Compute first order analytical derivatives of the model
fx11         = jacobian(f,x11);
fx11p        = jacobian(f,x11p);
fx12         = jacobian(f,x12);               %Accounting for change in c_cu at time t leads to change in c_ba1 at time t+1, say 
fy           = jacobian(f,y);
fy(:,1:myx)  = fy(:,1:myx) + jacobian(f,x12p); %Accounting for coding convention where c_ba1p = c_cu, say
fyp          = jacobian(f,yp);
%% Generating 'getModelDeriv.m'
% Folder were the file is stored
positionFiles     = [pwd,'\ModelSpecification'];

% Function for computing the Jacobiant of the model under certainty eq.
strucOfArrays = struct('f',f,'fx11',fx11,'fx11p',fx11p,'fy',fy,'fyp',fyp,'fx12',fx12);
nameOfFunction = 'getModelDeriv_EP.m';
generateModelDeriv_file(modelParams,x,y,strucOfArrays,positionFiles,nameOfFunction)

% Function for computing the model residuals under certainty eq.
nameOfFunction = 'DSGEforesight_N.m';
generateMfuncModel_DSGEforsight_N_file(modelParams,x,y,strucOfArrays,positionFiles,nameOfFunction)

% Function for computing the Euler equation errors on a grid or along a 
% simulated sample path
strucOfArrays = struct('f',fall);
nameOfFunction = 'EulerEqError.m';
generateModelEulerError_file(modelParams,x,y,xp,yp,strucOfArrays,positionFiles,nameOfFunction)
nameOfFunction = 'EulerEqError_mp.m';
generateModelEulerError_mp_file(modelParams,x,y,xp,yp,strucOfArrays,positionFiles,nameOfFunction)

% Function for computing the Euler equation errors with pruning along a simulated sample path
strucOfArrays = struct('f',fall);
nameOfFunction = 'EulerEqErrorPruning.m';
generateModelEulerErrorPruning_file(modelParams,x,y,xp,yp,strucOfArrays,positionFiles,nameOfFunction)
nameOfFunction = 'EulerEqErrorPruning_mp.m';
generateModelEulerErrorPruning_mp_file(modelParams,x,y,xp,yp,strucOfArrays,positionFiles,nameOfFunction)

